const uploadFileToQiniu = async (file) => {
  const key = `your-prefix/${file.name}` // 定义文件在七牛云存储的路径和文件名
  const token = await getQiniuToken() // 从后端获取上传凭证token
  const putExtra = {
    fname: file.name, // 文件原名
    params: {},
    mimeType: file.type // 文件类型
  }
  const config = {
    useCdnDomain: true,
    chunkSize: 4 * 1024 * 1024 // 分片大小，4MB
  }

  const observable = qiniu.upload(file, key, token, putExtra, config)
  let subscription = observable.subscribe({
    next: (res) => {
      console.log('上传进度', res.total.percent)
    },
    error: (err) => {
      console.error('上传失败', err)
    },
    complete: (res) => {
      console.log('上传成功', res)
    }
  })
  return subscription
}

const getQiniuToken = () => {
  // 从后端API获取token
  return fetch('/api/qiniu/token')
    .then((response) => response.json())
    .then((data) => data.token)
}
